package com.xunyin.modules.log.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xunyin.common.service.impl.BaseServiceImpl;
import com.xunyin.common.utils.QueryHelpPlus;
import com.xunyin.modules.log.domain.TCleaningRecord;
import com.xunyin.modules.log.service.TCleaningRecordService;
import com.xunyin.modules.log.service.dto.TCleaningRecordQueryCriteria;
import com.xunyin.modules.log.service.mapper.TCleaningRecordMapper;
import com.xunyin.pagination.PaginationRequest;
import com.xunyin.pagination.PaginationResponse;
import com.xunyin.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 保洁管理业务实现类
 * <p>
 * Created by xunyin
 * CopyRight 讯音云商 https://www.tonetn.com
 */
@Service
@RequiredArgsConstructor
@Slf4j
//@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class TCleaningRecordServiceImpl extends BaseServiceImpl<TCleaningRecordMapper, TCleaningRecord> implements TCleaningRecordService {

    @Autowired
    private TCleaningRecordMapper tCleaningRecordMapper;

    @Override
    public Map<String, Object> queryAll(TCleaningRecordQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<TCleaningRecord> page = new PageInfo<>(queryAll(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", page.getList());
        map.put("totalElements", page.getTotal());
        return map;
    }

    @Override
    public List<TCleaningRecord> queryAll(TCleaningRecordQueryCriteria criteria) {
        return baseMapper.selectList(QueryHelpPlus.getPredicate(TCleaningRecord.class, criteria));
    }


    /**
     * 小程序管理端获取保洁日志信息
     * @param tCleaningRecord
     * @return
     */
    public List<TCleaningRecord> queryCleaningRecordList(TCleaningRecord tCleaningRecord){
/*        LambdaQueryWrapper<TCleaningRecord> lambdaQueryWrapper = Wrappers.lambdaQuery();
        if (tCleaningRecord.getStoreId()!=null) {
            lambdaQueryWrapper.eq(TCleaningRecord::getStoreId, tCleaningRecord.getStoreId());
        }
        lambdaQueryWrapper.orderByDesc(TCleaningRecord::getId);*/
        List<TCleaningRecord> cleaningList = tCleaningRecordMapper.queryCleaningRecordList(tCleaningRecord.getStoreId());
        return cleaningList;
    }

    @Override
    public PaginationResponse<TCleaningRecord> queryCleaningRecordListByPagination(PaginationRequest paginationRequest) {
        Page<TCleaningRecord> pageHelper = PageHelper.startPage(paginationRequest.getCurrentPage(), paginationRequest.getPageSize());
        LambdaQueryWrapper<TCleaningRecord> lambdaQueryWrapper = Wrappers.lambdaQuery();

/*        String description = paginationRequest.getSearchParams().get("description") == null ? "" : paginationRequest.getSearchParams().get("description").toString();
        if (StringUtils.isNotBlank(description)) {
            lambdaQueryWrapper.like(MtBalance::getDescription, description);
        }
        String status = paginationRequest.getSearchParams().get("status") == null ? "" : paginationRequest.getSearchParams().get("status").toString();
        if (StringUtils.isNotBlank(status)) {
            lambdaQueryWrapper.eq(MtBalance::getStatus, status);
        }
        String userId = paginationRequest.getSearchParams().get("userId") == null ? "" : paginationRequest.getSearchParams().get("userId").toString();
        if (StringUtils.isNotBlank(userId)) {
            lambdaQueryWrapper.eq(MtBalance::getUserId, userId);
        }
        String orderSn = paginationRequest.getSearchParams().get("orderSn") == null ? "" : paginationRequest.getSearchParams().get("orderSn").toString();
        if (StringUtils.isNotBlank(orderSn)) {
            lambdaQueryWrapper.eq(MtBalance::getOrderSn, orderSn);
        }


 */
        String cleannerPhone = paginationRequest.getSearchParams().get("cleannerPhone") == null ? "" : paginationRequest.getSearchParams().get("cleannerPhone").toString();
        if (StringUtils.isNotBlank(cleannerPhone)) {
            lambdaQueryWrapper.eq(TCleaningRecord::getCleannerPhone, cleannerPhone);
        }
        String storeId = paginationRequest.getSearchParams().get("storeId") == null ? "" : paginationRequest.getSearchParams().get("storeId").toString();
        if (StringUtils.isNotBlank(storeId)) {
            lambdaQueryWrapper.eq(TCleaningRecord::getStoreId, storeId);
        }
        String storeIdList = paginationRequest.getSearchParams().get("storeIdList") == null ? "" : paginationRequest.getSearchParams().get("storeIdList").toString();
        if (StringUtils.isNotBlank(storeIdList)) {
            lambdaQueryWrapper.in(TCleaningRecord::getStoreId, JSONArray.parseArray(storeIdList, Integer.class));
        }
        lambdaQueryWrapper.orderByDesc(TCleaningRecord::getId);
        List<TCleaningRecord> cleaningList = baseMapper.selectList(lambdaQueryWrapper);

        PageRequest pageRequest = PageRequest.of(paginationRequest.getCurrentPage(), paginationRequest.getPageSize());
        PageImpl pageImpl = new PageImpl(cleaningList, pageRequest, pageHelper.getTotal());
        PaginationResponse<TCleaningRecord> paginationResponse = new PaginationResponse(pageImpl, TCleaningRecord.class);
        paginationResponse.setTotalPages(pageHelper.getPages());
        paginationResponse.setTotalElements(pageHelper.getTotal());
        paginationResponse.setContent(cleaningList);

        return paginationResponse;
    }


}
